package com.iwdnb.gkgz.port.service;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;

import com.iwdnb.bmnf.common.model.PageData;
import com.iwdnb.gkgz.common.model.dto.Stock;
import com.iwdnb.gkgz.infrastructure.dao.model.entity.StockPoolDO;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockPoolRepository;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockRepository;
import com.iwdnb.ssww.port.script.impl.MetaQueryPageServiceImpl;
import com.iwdnb.ssww.port.utils.MetaCommonBuilder;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StockPoolQueryPageService extends MetaQueryPageServiceImpl {
    @Autowired
    private StockPoolRepository stockPoolRepository;

    @Autowired
    private StockRepository stockRepository;

    @Override
    public PageData<JSONObject> queryPage(String objectCode, MetaQuery metaQuery) {
        List<StockPoolDO> stockPoolDOS = stockPoolRepository.findAll();
        List<String> codes = stockPoolDOS.stream().map(stock -> stock.getCode()).collect(
            Collectors.toList());
        List<Stock> stockList = stockRepository.findByCodes(codes);
        List<JSONObject> list = MetaCommonBuilder.objectToJSON(stockList);
        long size = CollectionUtils.isEmpty(list) ? 0 : list.size();
        return new PageData<>(size, list);
    }

}
